package test;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import org.xmap.app.App;
import org.xmap.ui.View;
import org.xmap.ui.base.Window;
import org.xmap.ui.control.Menu;
import org.xmap.ui.effect.Fading;
import org.xmap.ui.ViewManager;

public class FadingView extends View {
//    private int[][] rects;

    private Image[] imgs;
    private long showGuageTime = 0;

    public FadingView(View back) {
        super(back);
        this.setTitleText("测试淡入淡出");
//        this.setBgColor(0xffffff00);
//        this.setBgColor(0xffb5dff3);
//        ViewManager.instance.debugPrint("view w:" + this.getRect().width
//                                         + " h:" + this.getRect().height);
        this.setGuageText("正在联网", "", "请稍候", "");
        Menu m1 = new Menu();
        m1.addMenuItem("测试1");
        m1.addMenuItem("测试2");
        m1.addMenuItem("测试3");
        m1.addMenuItem("测试4");
        Menu m2 = new Menu();
        m2.addMenuItem("测试5");
        m2.addMenuItem("测试6");
        m2.addMenuItem("测试7");
        Menu m3 = new Menu();
        m3.addMenuItem("测试8");
        m3.addMenuItem("测试9");
        m3.addMenuItem("测试10");
        Menu m4 = new Menu();
        m4.addMenuItem("子菜单1", m1);
        m4.addMenuItem("子菜单2", m2);
        m4.addMenuItem("子菜单3", m3);
        Menu m5 = new Menu();
//        m5.addMenuItem("测试11");
//        m5.addMenuItem("测试12");
        m5.addMenuItem("弹出提示框");
        Menu m = new Menu();
        m.addMenuItem("子菜单", m4);
        m.addMenuItem("弹出提示框", m5);
        m.addMenuItem("弹出进度条");
        this.addLeftMenu("弹出提示框");
        this.addLeftMenu("弹出进度条");
        this.setLeftMenu(m);
        this.setRightMenu("返回");
        try {
            // Allocate mamory for the image array.
            // Use the getRGB method to get all ARGB values from the image to the rawInt array.
//            this.rects = new int[5][];
            this.imgs = new Image[5];
            for (int i = 0; i < 5; ++i) {
//                this.rects[i] = Fading.createTransparentCirque(0x0000FF, 5 + (i * 5), 3);
//                this.imgs[i] = Image.createRGBImage(this.rects[i], 16 + (i * 5 * 2), 16 + (i * 5 * 2), true);
                this.imgs[i] = Fading.createTransparentCirqueImage(0x0000FF, 5 + (i * 5), 3);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void commandEvent(String command, Window sender) {
        this.setTitleText("选中:" + command);
        if (command.equals("返回")) {
            ViewManager.instance.setView(this.back, View.ANIMATION_LEFT_TO_RIGHT);
        } else if (command.equals("弹出提示框")) {
            this.msgBox("确定", "返回", "弹出提示", "测试一下");
        } else if (command.equals("弹出进度条")) {
            this.showGuageTime = System.currentTimeMillis();
            this.showGuage();
        }
    }

    public void update() {
        if (System.currentTimeMillis() - this.showGuageTime > 3000) {
            this.hideGuage();
        }
    }

    public void draw(Graphics g) {
        for (int i = 0; i < 5; ++i) {
            g.drawImage(this.imgs[i], 30 * i, 50, Graphics.VCENTER | Graphics.HCENTER);
        }
    }
}
